package colthgame.app;

import java.util.Vector;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.MotionEvent;

public class ItemMgr
{
	private static ItemMgr	g_ItemMgr	= new ItemMgr();

	public static ItemMgr GetInstance()
	{
		return g_ItemMgr;
	}

	private ItemMgr()
	{
		m_vecCostumes = new Vector[ItemCostume.E_Layer.eMAX.ordinal()];
		m_cItemInitOffset = new Point[ItemCostume.E_Layer.eMAX.ordinal()];
		for (int nIndex = 0; nIndex < ItemCostume.E_Layer.eMAX.ordinal(); nIndex++)
		{
			m_vecCostumes[nIndex] = new Vector<ItemCostume>();
			m_cItemInitOffset[nIndex] = new Point(100, (50 * nIndex + 100));
		}
		m_eCurLayer = ItemCostume.E_Layer.eMAX;
	}

	public void AddCostume(ItemCostume.E_Layer _eLayer, Bitmap _bitmap)
	{
		ItemCostume costume = new ItemCostume(_eLayer);
		costume.SetBitmap(_bitmap);
		costume.SetInitPosX(m_nCharacterWidth + m_cItemInitOffset[_eLayer.ordinal()].x);
		costume.SetInitPosY(m_cItemInitOffset[_eLayer.ordinal()].y);
		m_cItemInitOffset[_eLayer.ordinal()].x += 20;
		m_vecCostumes[_eLayer.ordinal()].add(costume);
	}

	public ItemCostume GetCostume(ItemCostume.E_Layer _eLayer)
	{
		return m_vecCostumes[_eLayer.ordinal()].get(0);
	}

	public void surfaceChanged(int width, int height)
	{
		m_nSurfaceWidth = width;
		m_nSurfaceHeight = height;
	}

	public void SetCharacterWidth(int nWidth)
	{
		m_nCharacterWidth = nWidth;
	}

	public void Render(Canvas _canvas)
	{
		if (m_eCurLayer != ItemCostume.E_Layer.eMAX)
		{
			for (int nLayerIndex = 0; nLayerIndex < ItemCostume.E_Layer.eMAX.ordinal(); nLayerIndex++)
			{				
				for (int nIndex = 0; nIndex < m_vecCostumes[nLayerIndex].size(); nIndex++)
				{
					if( true == m_vecCostumes[nLayerIndex].get(nIndex).IsEquipped() )
					{
						m_vecCostumes[nLayerIndex].get(nIndex).Render(_canvas);
					}
				}
			}
			for (int nIndex = 0; nIndex < m_vecCostumes[m_eCurLayer.ordinal()].size(); nIndex++)
			{
				if( true == m_vecCostumes[m_eCurLayer.ordinal()].get(nIndex).IsEquipped() )
					continue;
				
				m_vecCostumes[m_eCurLayer.ordinal()].get(nIndex).Render(_canvas);
			}
		}
		else
		{
			for (int nLayerIndex = 0; nLayerIndex < ItemCostume.E_Layer.eMAX.ordinal(); nLayerIndex++)
			{				
				for (int nIndex = 0; nIndex < m_vecCostumes[nLayerIndex].size(); nIndex++)
				{
					m_vecCostumes[nLayerIndex].get(nIndex).Render(_canvas);
				}
			}
		}
	}

	public boolean onTouchEvent(MotionEvent event, Character _character)
	{
		if (m_eCurLayer != ItemCostume.E_Layer.eMAX)
		{
			for (int nIndex = m_vecCostumes[m_eCurLayer.ordinal()].size()-1; nIndex >= 0; nIndex--)
			{
				if (true == m_vecCostumes[m_eCurLayer.ordinal()].get(nIndex).onTouchEvent(event, _character))
					return true;
			}
		}
		else
		{
			for (int nLayerIndex = ItemCostume.E_Layer.eMAX.ordinal() - 1; nLayerIndex >= 0; nLayerIndex--)
			{
				for (int nIndex = m_vecCostumes[nLayerIndex].size()-1; nIndex >= 0; nIndex--)
				{
					if (true == m_vecCostumes[nLayerIndex].get(nIndex).onTouchEvent(event, _character))
						return true;
				}
			}
		}

		return true;
	}

	public void onItemClick(int _nposition)
	{
		if( ItemCostume.E_Layer.eMAX.ordinal() <= _nposition )
		{
			m_eCurLayer = ItemCostume.E_Layer.eMAX;
		}
		else
		{
			m_eCurLayer = ItemCostume.ELayer[_nposition];
		}
	}

	private Vector<ItemCostume>	m_vecCostumes[];
	private int					m_nSurfaceWidth;
	private int					m_nSurfaceHeight;
	private int					m_nCharacterWidth;
	private Point				m_cItemInitOffset[];
	private ItemCostume.E_Layer	m_eCurLayer;
}
